<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>内部问卷打印存档</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <iframe ref="iframeExcel" :src="$questionnairePrint" frameborder="0" class="iframeShow"></iframe>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">关闭</el-button>
        <el-button @click="rechFn" size="small">刷新</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from "vuex";

export default {
  props: ['visible','detailId'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      dialogVisible: false,
      iframeWin: '',
      studentInfo: [],
      detailOption: null,
      iframeStatus: false
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  watch: {
    visible() {
      let n =this
      this.dialogVisible = this.visible
      if(this.visible){
        this.Inint()
        this.$nextTick(function () {
          // window.addEventListener('message', n.getFileFn);
          window.onmessage = n.getFileFn
          n.iframeWin = n.$refs.iframeExcel.contentWindow;
        })
      }
    }
  },
  created() {
  },
  methods: {
    // 问卷详情
    Inint() {
      let n = this
      this.$fetch(this.api.questionnaire_inside_mydetail, {id: this.detailId}).then((response) => {
        if (response.code === 0) {
          this.listCheck = response.list
          for (let i in response.list) {
            if (response.list[i].type !== 3) {
              for (let j in response.list[i].option) {
                response.list[i].option[j]['checked'] = false
                if (response.list[i].option[j].type == 1) {
                  response.list[i].option[j]['word'] = ''
                }
              }
            } else {
              response.list[i].option.push({word: ''})
            }
          }
          if(response.sign){
            response.sign.pic = `${this.$BASE_URL}${response.sign.pic}`
          }
          let data_ = {
            sign: response.sign,
            list: response.list,
            detail: response.vo,
            userinfo: {name: this.userinfo.realname, id: this.userinfo.id}
          }
          this.detailOption = JSON.parse(JSON.stringify(data_))
          console.log('response.list========>:', this.detailOption)
          if(this.iframeStatus){
            this.verificationFrom()
          }
          // this.detailCheckFn()
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    getFileFn(event) {
      let {data} = event.data
      console.log('收到页面传输的数据回来:', data)
      if (!data) {
        return
      }
      // type init 页面完成加载可以进行数据传输
      if(data.type === 'init'){
        this.iframeStatus = true
      }
      if (data.type === 'init' && this.detailOption) {
        this.verificationFrom()
      }
    },
    rechFn(){
      document.querySelector('iframe').contentWindow.location.reload();
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    // 内部问卷数据传输
    verificationFrom() {
      console.log('传输内容')
      let n = this
      this.iframeWin.postMessage({
        method: '打印',
        data: {data: JSON.stringify(n.detailOption)},
        type: 'data' // type 1 需要上传视频资源 2 关闭重置页面内容
      }, '*')
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 640px;
  height: 90%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}
.iframeShow{
  width:100%;
  height: 100%;
}
</style>


